import { getToken } from '@/utils/storage'
import { Message } from 'element-ui'
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    // { path: '/', redirect: '/dashboard' },
    {
      path: '/',
      component: () => import('@/views/layout'),
      redirect: '/dashboard',
      children: [
        // 二级路由的path，
        // 1.绝对路径
        // 2.相对路径 =》会自动拼接上父路由地址 =》/dashboard
        { path: 'dashboard', component: () => import('@/views/dashboard') },
        { path: 'article', component: () => import('@/views/article') }
      ]
    },
    { path: '/login', component: () => import('@/views/login') }
  ]
})
const whileList = ['/login']
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token || whileList.includes(to.path)) return next()
  next('/login')
  Message.error('请先登录再访问')
})

export default router
